package com.example.pomelo_home.service.impl;

import com.example.pomelo_home.dao.OrderRepository;
import com.example.pomelo_home.dto.OrderCountByDateDto;
import com.example.pomelo_home.dto.OrderDTO;
import com.example.pomelo_home.entity.Order;
import com.example.pomelo_home.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderRepository orderRepository;

    @Override
    public List<OrderCountByDateDto> getCountByDateRange() {
        // 计算日期范围
        Calendar calendar = Calendar.getInstance();
        Date today = calendar.getTime();
        calendar.add(Calendar.DAY_OF_MONTH, -15); // 前15天
        Date startDate = calendar.getTime();

        // 调用自定义查询
        List<Object[]> results = orderRepository.countOrdersByDate(startDate, today);

        // 处理查询结果
        List<OrderCountByDateDto> dtoList = new ArrayList<>();
        for (Object[] result : results) {
            Date orderDate = (Date) result[0]; // 这里实际上是java.sql.Date，但可以作为Date使用
            Long count = (Long) result[1];

            // 转换为所需的DTO格式（如果需要，可以格式化日期）
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = dateFormat.format(orderDate);

            dtoList.add(new OrderCountByDateDto(formattedDate, count));
        }

        return dtoList;
    }

    @Override
    public Page<Order> findAllByPaged(Integer page, Integer size) {
        Pageable pageable= PageRequest.of(page-1,size);
        Page<Order> all=orderRepository.findAll(pageable);
        return all;
    }

    @Override
    public Page<Order> findByLike(OrderDTO orderDTO) {
        Pageable pageable= PageRequest.of(orderDTO.getPage()-1,orderDTO.getSize());
        Page<Order> all=orderRepository.findByLike(orderDTO.getOrderStatus(),orderDTO.getActivityTitle(),pageable);
        return all;
    }

    @Override
    public int audit(Integer orderId) {
        return orderRepository.audit(orderId);
    }

    @Override
    public int deleteOrder(Integer orderId) {
        return orderRepository.deleteOrder(orderId);
    }

    @Override
    public int Application(OrderDTO orderDTO) {
        orderDTO.setApplicationTime(new Date());
        return orderRepository.Application(orderDTO.getActivityTitle(),orderDTO.getOpenid(),orderDTO.getName(),orderDTO.getPhone(),orderDTO.getPaymentAmount(),orderDTO.getApplicationTime());
    }

    @Override
    public List<Order> findAllOrder() {
        return orderRepository.findAllOrder();
    }

    @Override
    public int delete(Integer orderId) {
        return orderRepository.delete(orderId);
    }

    @Override
    public int cancel(Integer orderId) {
        return orderRepository.cancel(orderId);
    }

    @Override
    public int pay(Integer orderId) {
        return orderRepository.pay(orderId);
    }

}
